package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.floorMr.SundayFloorDetailDto;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXInFloorMrGroupService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/7/18
 * @Description：
 */
@Controller
@Api(value = "FloorMr",description = "楼先生")
@RequestMapping("/sunday/small/floorMr")
public class SundaySmallFloorMrController extends BaseController{
    @Autowired
    private SundayWeiXInFloorMrGroupService groupService;

    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ApiOperation("获取楼先生列表")
    public void select(HttpServletRequest request, HttpServletResponse response
            ,@ApiParam(value = "起始页",required = true) @RequestParam(value = "pageNumber",required = true) String pageNumber
            , @ApiParam(value = "每页大小",required = true) @RequestParam(value = "pageSize",required = true)String pageSize){
        try {
            ResponseMessage.success( groupService.select(request));
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/floorMr/select----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }
    /*@ApiOperation("【小程序】根据楼先生id获取详细问答")
    @RequestMapping(value = "/getDetail",method = {RequestMethod.POST,RequestMethod.GET})
    public String getDetail(HttpServletRequest request,HttpServletResponse response
            ,@ApiParam(value = "楼先生问题组id",required = true)@RequestParam(value = "groupId")Long groupId
            ,@ApiParam(value = "statusBarHeight",required = false)@RequestParam(value = "statusBarHeight")Long statusBarHeight
            ,@ApiParam(value = "titleBarHeight",required = false)@RequestParam(value = "titleBarHeight")Long titleBarHeight
            ,Model model
           ) {
            try {
            model.addAttribute("statusBarHeight", statusBarHeight);
            model.addAttribute("titleBarHeight", titleBarHeight);
            Long totalHeight=Long.valueOf(statusBarHeight)+Long.valueOf(titleBarHeight);
            model.addAttribute("totalHeight",totalHeight);
            model.addAttribute("floorMr",groupService.getDetail(groupId));
           return "/sunday/floorMr/group/html";
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/floorMr/getDetail---e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
        return "/sunday/error";
    }*/
    @ApiOperation("【小程序】根据楼先生id获取详细问答")
    @RequestMapping(value = "/getDetail",method = {RequestMethod.POST})
    public void getDetail(HttpServletRequest request,HttpServletResponse response
            ,@ApiParam(value = "楼先生问题组id",required = true)@RequestParam(value = "groupId")Long groupId
            ,Model model
           ) {
        try {
            ResponseMessage.success(groupService.getDetail(groupId));
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/floorMr/getDetail---e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }
}
